package com.fly.elasticsearch.essql;

import org.elasticsearch.xpack.sql.jdbc.EsDriver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcExample {
    /*
        1. 加载ES驱动
        2. 建立连接
        3. 准备SQL语句
        4. 使用PreparedStatement执行SQL
        5. 遍历结果
        6. 关闭连接
     */
    public static void main(String[] args) throws Exception {
        Class.forName(EsDriver.class.getName());
        String url = "jdbc:es://http://192.168.199.144:9200";
//        String url = "jdbc:es://192.168.199.144:9200";
        Connection connection = DriverManager.getConnection(url);

        String sql = "select payway, count(*) cnt from order_idx group by payway";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            byte payway = resultSet.getByte("payway");
            long cnt = resultSet.getLong("cnt");
            System.out.println("支付方式: " + payway + " 订单数量: " + cnt);
        }

        resultSet.close();
        connection.close();
    }
}
